﻿#include "videocall.h"
#include "ui_videocall.h"
#include "login.h"

VideoCall::VideoCall(QWidget *parent,QString id) :
    QWidget(parent),
    ui(new Ui::VideoCall)
{
    ui->setupUi(this);
    ui->btn_stop_2->hide();
    this->id = id;
    m_pTcpSock = new QTcpSocket(this);
    m_pTcpSock->connectToHost(QString("192.168.43.91"),10088);

    connect(m_pTcpSock,&QTcpSocket::connected,this,[=](){
        ui->label->setStyleSheet("background-color:blue;");
        m_pTcpSock->write(id.toUtf8(),id.toUtf8().length()+1);//sen'd
        _sleep(500);
        //连上 才初始化设备
        //初始化音频设备
        input_stream = nullptr;
        output_stream = nullptr;
        //获取音频设备
        input_Device = QAudioDeviceInfo::defaultInputDevice();
        output_Device = QAudioDeviceInfo::defaultOutputDevice();
        //设置音频格式
        settings.setCodec("audio/pcm");
        settings.setSampleRate(44100);
        settings.setSampleType(QAudioFormat::UnSignedInt);
        settings.setSampleSize(16);
        settings.setChannelCount(1);
        settings.setByteOrder(QAudioFormat::LittleEndian);
        //格式支持判断,若不支持则选择相近格式
        if(!input_Device.isFormatSupported(settings))
            input_Device.nearestFormat(settings);
        if(!output_Device.isFormatSupported(settings))
            output_Device.nearestFormat(settings);
        //开始录音和监听
        qDebug()<<"start recording...";
        audioInput = new QAudioInput(input_Device,settings,this);
        audioOutput = new QAudioOutput(output_Device,settings,this);
        audioInput->setBufferSize(4000);
        audioOutput->setBufferSize(4000);
        //将QIODevice指向输入流和输出流
        input_stream = audioInput->start();
        output_stream = audioOutput->start();
        input_stream->open(QIODevice::ReadOnly);
        output_stream->open(QIODevice::WriteOnly);

        connect(input_stream,&QIODevice::readyRead,
                [=](){
            auto data = input_stream->readAll();
            m_pTcpSock->write(data);
        });
        //
        connect(m_pTcpSock,&QTcpSocket::readyRead,this,[=](){
            auto data = m_pTcpSock->readAll();
            output_stream->write(data);
        });


        thread = new  VideoCallThread(this,id);

        connect(thread,&VideoCallThread::sendImg,[=](QImage img){
            QPixmap pixmap = QPixmap::fromImage(img);
            pixmap.scaled(ui->label_showself->size(), Qt::KeepAspectRatio);
            ui->label_showself->setScaledContents(true);
            ui->label_showself->setPixmap(pixmap);
            ui->label_showself->setScaledContents(true);
        });

        connect(thread,&VideoCallThread::sendImg2,[=](QByteArray ba){
            QImage img = DataToImage(ba);
            QPixmap pixmap = QPixmap::fromImage(img);
            pixmap.scaled(ui->label_showimg->size(), Qt::KeepAspectRatio);
            ui->label_showimg->setScaledContents(true);
            ui->label_showimg->setPixmap(pixmap);
            ui->label_showimg->setScaledContents(true);
        });
        connect(this,&VideoCall::startImgCall,[=](){
            thread->run();
        });
        connect(this,&VideoCall::stopImgCall,[=](){
            thread->exit(0);
        });
    });


}

VideoCall::~VideoCall()
{
    emit stopImgCall();
    m_pTcpSock->close();
    audioInput->stop();
    audioOutput->stop();
    delete ui;
}

void VideoCall::setLabelInfo(QString info)
{
    ui->label_info->setText(info);
}

void VideoCall::sendSignalImgCall()
{
    emit startImgCall();
}

QByteArray VideoCall::ImageToData(const QImage &image)
{
    QByteArray imageData;
    QBuffer buffer(&imageData);
    image.save(&buffer, "jpg");
    imageData = imageData.toBase64();
    return imageData;
}

QImage VideoCall::DataToImage(const QString &data)
{
    QByteArray imageData = QByteArray::fromBase64(data.toLatin1());
    QImage image;
    image.loadFromData(imageData);
    return image;
}




void VideoCall::on_btn_stop_clicked()//挂断
{
    m_pTcpSock->close();
    audioInput->stop();
    audioOutput->stop();
    close();
    destroy();
}

void VideoCall::on_btn_stop_2_clicked()//接听
{

}
